package com.ylbh.common.utils;

import org.apache.commons.lang.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 将富文本内容以html格式保存
 */
public class UploadHtmlFile {
    /**
     * 上传HTML文件到服务器
     * 思路：先将文件保存到本地路径下面，然后调用工具列ossClientUtil中的
     * uploadImg2Oss方法，将本地文件上传到服务器中，然后返回上传的路径即可
     * @param msg
     * @return
     */
    public static String uploadHtmlOss(String msg, String fileName){
        System.out.println(fileName);
        if (StringUtils.isNotBlank(msg)) {
            InputStream is = null;
            try {

                String content = "<!DOCTYPE html>\n" +
                        "<html lang=\"zh-cn\">\n" +
                        "\t<head>\n" +
                        "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
                        "\t\t<meta http-equiv=\"Content-Type\">\n" +
                        "\t\t<meta content=\"text/html; charset=utf-8\">\n" +
                        "\t\t<meta charset=\"utf-8\">\n" +
                        "\t\t<meta name=\"viewport\" content=\"initial-scale=1, maximum-scale=1, user-scalable=no\">\n" +
                        "\t\t<meta name=\"format-detection\" content=\"telephone=no\">\n" +
                        "\t\t<meta name=\"format-detection\" content=\"email=no\">\n" +
                        "\t\t<meta name=\"referrer\" content=\"never\">\n" +
                        "\t\t<title></title>\n" +
                        "\t\t<style>\n" +
                        "\t\t\t* {\n" +
                        "\t\t\t\tmargin: 0;\n" +
                        "\t\t\t\tpadding: 0;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\t*:not(input, textarea) {\n" +
                        "\t\t\t\t/* -webkit-touch-callout: inherit;\n" +
                        "\t\t\t  -webkit-user-select: auto; */\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tbody {\n" +
                        "\t\t\t\twidth: 100%;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\t/*设置IOS页面长按不可复制粘贴，但是IOS上出现input、textarea不能输入，因此将使用-webkit-user-select:auto;*/\n" +
                        "\t\t\t* {\n" +
                        "\t\t\t\t-webkit-touch-callout: none;\n" +
                        "\t\t\t\t/*系统默认菜单被禁用*/\n" +
                        "\t\t\t\t-webkit-user-select: none;\n" +
                        "\t\t\t\t/*webkit浏览器*/\n" +
                        "\t\t\t\t-khtml-user-select: none;\n" +
                        "\t\t\t\t/*早期浏览器*/\n" +
                        "\t\t\t\t-moz-user-select: none;\n" +
                        "\t\t\t\t/*火狐*/\n" +
                        "\t\t\t\t-ms-user-select: none;\n" +
                        "\t\t\t\t/*IE10*/\n" +
                        "\t\t\t\tuser-select: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tinput,\n" +
                        "\t\t\ttextarea {\n" +
                        "\t\t\t\t-webkit-user-select: auto;\n" +
                        "\t\t\t\t/*webkit浏览器*/\n" +
                        "\t\t\t\tmargin: 0px;\n" +
                        "\t\t\t\tpadding: 0px;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ta {\n" +
                        "\t\t\t\tcolor: #878787;\n" +
                        "\t\t\t\ttext-decoration: none;\n" +
                        "\t\t\t\t-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ta:hover {\n" +
                        "\t\t\t\ttext-decoration: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tbutton,\n" +
                        "\t\t\tinput,\n" +
                        "\t\t\tselect,\n" +
                        "\t\t\ttextarea {\n" +
                        "\t\t\t\tfont-size: 100%;\n" +
                        "\t\t\t\tmargin: 0;\n" +
                        "\t\t\t\tpadding: 0;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tdt,\n" +
                        "\t\t\tdd {\n" +
                        "\t\t\t\tdisplay: inline-block;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ttextarea,\n" +
                        "\t\t\tinput {\n" +
                        "\t\t\t\tresize: none;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ttextarea {\n" +
                        "\t\t\t\tresize: none;\n" +
                        "\t\t\t\t-webkit-appearance: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tul,\n" +
                        "\t\t\tol,\n" +
                        "\t\t\tli {\n" +
                        "\t\t\t\tlist-style: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tem {\n" +
                        "\t\t\t\tfont-style: normal;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\thtml,\n" +
                        "\t\t\tbody {\n" +
                        "\t\t\t\t-webkit-text-size-adjust: 100%;\n" +
                        "\t\t\t\t-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\timg {\n" +
                        "\t\t\t\tmax-width: 100% !important;\n" +
                        "\t\t\t\theight: auto !important;\n" +
                        "\t\t\t}\n" +
                        "\t\t\t.rich-txt{\n" +
                        "\t\t\t\tpadding: 15px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\tsection {\n" +
                        "\t\t\t\tmax-width: 100% !important;\n" +
                        "\t\t\t}\n" +
                        "\t\t\tvideo {\n" +
                        "\t\t\t\twidth: 100% !important;\n" +
                        "\t\t\t\theight: auto !important;\n" +
                        "\t\t\t}\n" +
                        "\t\t</style>\n" +
                        "\t</head>\n" +
                        "\t<body>\n" +
                        "\t\t<div class=\"rich-txt\">" + msg +
                        "</div>\n" +
                        "\t</body>\n" +
                        "</html>\n";
                is = new ByteArrayInputStream(content.getBytes());
                String url = new OSSClientUtil().uploadFile2OSS1(is, System.currentTimeMillis() + ".html", fileName);
                is.close();
                return url;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

    public static String uploadHtmlOss2(String msg, String fileName){
        if (StringUtils.isNotBlank(msg)) {
            InputStream is = null;
            try {
                String content = "<!DOCTYPE html>\n" +
                        "<html lang=\"zh-cn\">\n" +
                        "\t<head>\n" +
                        "\t\t<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" +
                        "\t\t<meta http-equiv=\"Content-Type\">\n" +
                        "\t\t<meta content=\"text/html; charset=utf-8\">\n" +
                        "\t\t<meta charset=\"utf-8\">\n" +
                        "\t\t<meta name=\"viewport\" content=\"initial-scale=1, maximum-scale=1, user-scalable=no\">\n" +
                        "\t\t<meta name=\"format-detection\" content=\"telephone=no\">\n" +
                        "\t\t<meta name=\"format-detection\" content=\"email=no\">\n" +
                        "\t\t<title></title>\n" +
                        "\t\t<style>\n" +
                        "\t\t\t* {\n" +
                        "\t\t\t\tmargin: 0;\n" +
                        "\t\t\t\tpadding: 0;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\t*:not(input, textarea) {\n" +
                        "\t\t\t\t/* -webkit-touch-callout: inherit;\n" +
                        "\t\t\t  -webkit-user-select: auto; */\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tbody {\n" +
                        "\t\t\t\twidth: 100%;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\t/*设置IOS页面长按不可复制粘贴，但是IOS上出现input、textarea不能输入，因此将使用-webkit-user-select:auto;*/\n" +
                        "\t\t\t* {\n" +
                        "\t\t\t\t-webkit-touch-callout: none;\n" +
                        "\t\t\t\t/*系统默认菜单被禁用*/\n" +
                        "\t\t\t\t-webkit-user-select: none;\n" +
                        "\t\t\t\t/*webkit浏览器*/\n" +
                        "\t\t\t\t-khtml-user-select: none;\n" +
                        "\t\t\t\t/*早期浏览器*/\n" +
                        "\t\t\t\t-moz-user-select: none;\n" +
                        "\t\t\t\t/*火狐*/\n" +
                        "\t\t\t\t-ms-user-select: none;\n" +
                        "\t\t\t\t/*IE10*/\n" +
                        "\t\t\t\tuser-select: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tinput,\n" +
                        "\t\t\ttextarea {\n" +
                        "\t\t\t\t-webkit-user-select: auto;\n" +
                        "\t\t\t\t/*webkit浏览器*/\n" +
                        "\t\t\t\tmargin: 0px;\n" +
                        "\t\t\t\tpadding: 0px;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ta {\n" +
                        "\t\t\t\tcolor: #878787;\n" +
                        "\t\t\t\ttext-decoration: none;\n" +
                        "\t\t\t\t-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ta:hover {\n" +
                        "\t\t\t\ttext-decoration: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tbutton,\n" +
                        "\t\t\tinput,\n" +
                        "\t\t\tselect,\n" +
                        "\t\t\ttextarea {\n" +
                        "\t\t\t\tfont-size: 100%;\n" +
                        "\t\t\t\tmargin: 0;\n" +
                        "\t\t\t\tpadding: 0;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tdt,\n" +
                        "\t\t\tdd {\n" +
                        "\t\t\t\tdisplay: inline-block;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ttextarea,\n" +
                        "\t\t\tinput {\n" +
                        "\t\t\t\tresize: none;\n" +
                        "\t\t\t\toutline: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\ttextarea {\n" +
                        "\t\t\t\tresize: none;\n" +
                        "\t\t\t\t-webkit-appearance: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tul,\n" +
                        "\t\t\tol,\n" +
                        "\t\t\tli {\n" +
                        "\t\t\t\tlist-style: none;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\tem {\n" +
                        "\t\t\t\tfont-style: normal;\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\thtml,\n" +
                        "\t\t\tbody {\n" +
                        "\t\t\t\t-webkit-text-size-adjust: 100%;\n" +
                        "\t\t\t\t-webkit-tap-highlight-color: rgba(0, 0, 0, 0);\n" +
                        "\t\t\t}\n" +
                        "\n" +
                        "\t\t\timg {\n" +
                        "\t\t\t\tmax-width: 100% !important;\n" +
                        "\t\t\t\theight: auto !important;\n" +
                        "\t\t\t\tdisplay: block;\n" +
                        "\t\t\t}\n" +
                        "\t\t\t.rich-txt{\n" +
                        "\t\t\t\tpadding: 15px;\n" +
                        "\t\t\t}\n" +
                        "\t\t\tsection {\n" +
                        "\t\t\t\tmax-width: 100% !important;\n" +
                        "\t\t\t}\n" +
                        "\t\t\tvideo {\n" +
                        "\t\t\t\twidth: 100% !important;\n" +
                        "\t\t\t\theight: auto !important;\n" +
                        "\t\t\t}\n" +
                        "\t\t</style>\n" +
                        "\t</head>\n" +
                        "\t<body>\n" +
                        "\t\t<div class=\"rich-txt\">" + msg +
                        "</div>\n" +
                        "\t</body>\n" +
                        "</html>\n";
                is = new ByteArrayInputStream(content.getBytes());
                String url = new OSSClientUtil().uploadFile2OSS1(is, System.currentTimeMillis() + ".html", fileName);
                is.close();
                return url;
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
        return null;
    }

}
